package com.lmscn.lms.api.controller;

import com.jfinal.core.Controller;
import com.lmscn.lms.api.model.*;
import com.lmscn.lms.api.util.CommonUtil;
import com.lmscn.lms.api.util.JsonKit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by xiaojie on 15-11-11.
 */
public class CountController extends Controller {

    /**
     * 学习记录
     */

    public void onlineCount() throws ParseException {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        Long studentId = getParaToLong("studentId");
        String pageNum = getPara("num");
        if (pageNum == null || pageNum.equals("")) {
            pageNum = "0";
        }
        int num = Integer.valueOf(pageNum);
        int days = 5;
        Map onlineList = new HashMap();
        Map playList = new HashMap();
        Map playNumberList = new HashMap();
        float yMax = 0;
        float onlineCount = 0;
        for (int i = 0; i < days; i++) {   //查询5天的数据
            Calendar now = Calendar.getInstance();
            now.set(Calendar.DATE, now.get(Calendar.DATE) - i + num * 5);
            int year = now.get(Calendar.YEAR);
            int month = now.get(Calendar.MONTH) + 1;
            int day = now.get(Calendar.DAY_OF_MONTH);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            float onlineTime = 0;
            float playTime = 0;
            int playNumber;
            Date beginDate = sdf.parse(year + "-" + month + "-" + day);
            Calendar cal = Calendar.getInstance();
            cal.setTime(beginDate);
            cal.add(Calendar.DATE, 1);
            Date endDate = cal.getTime();


            String monthStr = String.valueOf(month);
            String dayStr = String.valueOf(day);
            String months;
            String daystring;
            if (monthStr.length() < 2) {
                months = "0" + monthStr;
            } else {
                months = monthStr;
            }
            if (dayStr.length() < 2) {
                daystring = "0" + dayStr;
            } else {
                daystring = dayStr;
            }
            List<OnlineCount> list = OnlineCount.dao.
                    findByStudentIdAndDate(studentId, String.valueOf(year), months, daystring);


            List<LessonStudentRecord> lessonStudentRecordList = LessonStudentRecord.dao.
                    findByStartTimeToNumber(studentId, beginDate, endDate);
            for (int j = 0; j < lessonStudentRecordList.size(); j++) {//统计播放时长
                playTime += lessonStudentRecordList.get(j).getInt("learned_hour");
            }
            onlineCount += (float)playTime / 60;
            for (int j = 0; j < list.size(); j++) {//统计在线时长
                onlineTime += (float)list.get(j).getLong("online_time");
            }
            playNumber = lessonStudentRecordList.size();//统计播放节数

            onlineList.put("onlineTime" + i, (float)onlineTime / 60);
            playList.put("playTime" + i, (float)playTime / 60);

            if (yMax < onlineTime / 60) //生成Y轴最大值
                yMax = onlineTime / 60;
            if (yMax < playTime / 60)
                yMax = playTime / 60;
            if (yMax < playNumber)
                yMax = playNumber;

            playNumberList.put("playNumber" + i, playNumber);
            dataMap.put("date" + i, month + "-" + day);
        }
        dataMap.put("Y", yMax);
        dataMap.put("onlineTime", onlineList);
        dataMap.put("playTime", playList);
        dataMap.put("playNumber", playNumberList);
        dataMap.put("onlineCount", onlineCount);
        renderJson(JsonKit.mapToJson(dataMap, 39));
    }

    /**
     * 记录每天登录时长
     * 
     * 登录时调用了；但是未发现beginTime参数的处理
     * 然后不知道什么时候又调用了一次，传入了endTime参数；
     * @date 2016年11月10日11:12:42
     */
    public void onlineTime() {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        Long studentId = getParaToLong("studentId");
        String endTime = getPara("endTime");
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String str = sdf.format(date);
        if (endTime == null || "".equals(endTime)) {
            OnlineCount onlineCount = getModel(OnlineCount.class);
            OnlineCount.dao.save(onlineCount, str.substring(0, 4), str.substring(5, 7), str.substring(8, 10), date, studentId);
        } else {
            OnlineCount ol = OnlineCount.dao.findbyStudentId(studentId);
            if (ol.getDate("endTime") == null) {
                ol.set("endTime", date);
                double time = date.getTime() - ol.getDate("beginTime").getTime();
                ol.set("online_time", time / 1000);
                ol.update();
            }
        }
        dataMap.put("msg", "success");
        renderJson(JsonKit.mapToJson(dataMap, 39));
    }


    /**
     * 学习数据统计
     */
    public void learningData() {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        Long studentId = getParaToLong("studentId");
        List<CourseStudent> list = CourseStudent.dao.findByStudentId(studentId);
        double dj = 0l;
        double jj = 0l;
        double zz = 0l;
        double wh = 0l;
        double xw = 0l;
        double kj = 0l;
        double ss = 0l;
        double stwm = 0l;
        for (int i = 0; i < list.size(); i++) {
            CourseBase courseBase = CourseBase.dao.findById(list.get(i).getLong("course_id"));
            CourseLearningSetting courseLearningSetting = courseBase.getCourseLearningSetting();
            if (courseBase != null) {
                if (courseBase.get("label").equals("1")) {
                    dj += courseLearningSetting.getFloat("period");
                } else if (courseBase.get("label").equals("2")) {
                    jj += courseLearningSetting.getFloat("period");
                } else if (courseBase.get("label").equals("3")) {
                    zz += courseLearningSetting.getFloat("period");
                } else if (courseBase.get("label").equals("4")) {
                    wh += courseLearningSetting.getFloat("period");
                } else if (courseBase.get("label").equals("5")) {
                    xw += courseLearningSetting.getFloat("period");
                } else if (courseBase.get("label").equals("6")) {
                    kj += courseLearningSetting.getFloat("period");
                } else if (courseBase.get("label").equals("7")) {
                    ss += courseLearningSetting.getFloat("period");
                } else if (courseBase.get("label").equals("8")) {
                    stwm += courseLearningSetting.getFloat("period");
                }
            }

        }
        dataMap.put("dj", dj);
        dataMap.put("jj", jj);
        dataMap.put("zz", zz);
        dataMap.put("wh", wh);
        dataMap.put("xw", xw);
        dataMap.put("kj", kj);
        dataMap.put("ss", ss);
        dataMap.put("stwm", stwm);
        dataMap.put("learningData", list.size());
        renderJson(JsonKit.mapToJson(dataMap, 39));
    }

    /**
     * 学时统计
     */
    public void periodCount() {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        Long studentId = getParaToLong("studentId");
        List<CourseStudent> list = CourseStudent.dao.findByStudentId(studentId);
        float notFinish = 0;
        float elective = 0;
        float obligatory = 0;
        for (int i = 0; i < list.size(); i++) {
            CourseLearningSetting courseLearningSetting = CourseLearningSetting.dao.findByCourseId(list.get(i).getLong("course_id"));
            if (courseLearningSetting != null) {
                if (courseLearningSetting.get("type").equals("0")) { // 选修
                    elective += courseLearningSetting.getFloat("period");
                } else {
                    obligatory += courseLearningSetting.getFloat("period");
                }
                if (list.get(i).getInt("learned_status") == 0) {// 未完成学时
                    notFinish += courseLearningSetting.getFloat("period");
                }
            }

        }
        float exam = 0;
        List<MyExamStudent> myExamList = MyExamStudent.dao.findByStudentId(studentId, 1);
        for (int i = 0; i < myExamList.size(); i++) {
            MyExamLearningSetting myExamLearningSetting = MyExamLearningSetting.dao.
                    findByMyExamId(myExamList.get(i).getLong("myexam_id"));
            if (myExamLearningSetting != null) {
                exam += myExamLearningSetting.getFloat("required_period");
            }
        }
        dataMap.put("ks", exam);
        dataMap.put("wwc", notFinish);
        dataMap.put("xx", elective);
        dataMap.put("bx", obligatory);
        dataMap.put("periodCount", exam + notFinish + elective + obligatory);
        renderJson(JsonKit.mapToJson(dataMap, 39));
    }

    public void url() {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        String url = CommonUtil.imgUrl;
        dataMap.put("url", url);
        renderJson(JsonKit.mapToJson(dataMap, 39));
    }
}
